package com.nl.controller.web.controller;


import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsContent;
import com.nl.controller.web.bean.SmsTemplateAttr;
import com.nl.controller.web.bean.SmsUsers;

import com.nl.controller.web.mapper.SmsTemplateAttrMapper;
import com.nl.controller.web.mapper.SmsUpwardMapper;
import com.nl.controller.web.service.SmsBusiService;
import com.nl.controller.web.service.SmsContentService;
import com.nl.controller.web.service.SmsTemplateAttrService;
import com.nl.controller.web.service.SmsTemplateCheckService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("templateCheck")
public class SmsTemplateCheckController {
    @Autowired
    private SmsTemplateCheckService smsTemplateCheckService;
    @Autowired
    private SmsUpwardMapper smsUpwardMapper;
    @Autowired
    private SmsBusiService smsBusiService;
	@Autowired
	private SmsTemplateAttrMapper smsTemplateAttrMapper;
	@Autowired
	private SmsContentService smsContentService;
	@Autowired
	private SmsTemplateAttrService smsTemplateAttrService;

    @ApiOperation(value = "模板审核", notes = "模板审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "审核结果", required = true, dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "审核描述", required = false, dataType = "String"),
    })
    @PostMapping("check")
    public Response checkTemlplate(
            @RequestParam(value = "mgrId", required = true) String mgrId,
            @RequestParam(value = "state", required = true) String state,
            @RequestParam(value = "remark", required = true) String remark,
            HttpSession session,
            @RequestParam Map<String, Object> paramMap
    ) {
        int result;
        log.info("模板审核参数:{}" + paramMap.toString());
        SmsUsers users = (SmsUsers) session.getAttribute("user");
        //地市管理员审核，通过入表发送系统管理员，不通过入表发送操作人
        if("1".equals(users.getDuty())){
        	SmsTemplateAttr smsTemplateAttr = smsTemplateAttrMapper.findTemplateAttrByMgrId(mgrId);
        	//待发表信息
        	 Map<String, Object> mapSend = new HashMap<String, Object>();
				if ("11".equals(smsTemplateAttr.getCityId())) {
					mapSend.put("sender", "106571103");
				}else {
					mapSend.put("sender", "106571102");
				}
				mapSend.put("result", -1);
				mapSend.put("cityId", smsTemplateAttr.getCityId());
				mapSend.put("mgrId", mgrId);
				mapSend.put("mgrName", smsTemplateAttr.getName());
				mapSend.put("cityExtId", smsTemplateAttr.getCityExtId());
				
           if("3".equals(state)){
        	   StringBuffer sBuffer = new StringBuffer();
				sBuffer.append("您好,项目:");
				sBuffer.append(smsTemplateAttr.getName());
				sBuffer.append("由"+users.getMsisdn()+"审核;");
				sBuffer.append("审核结果:失败;");
				//查询项目操作人
				String operatorMsisdn = smsContentService.findOperatorMsisdn(mgrId);
				try {
					mapSend.put("receiver", operatorMsisdn);
					mapSend.put("content", sBuffer.toString());
					result = smsTemplateAttrService.insertSend(mapSend);
					String logString = result==1 ? "短信发送成功" : "短信发送失败";
					log.info(logString+";短信内容为:"+sBuffer.toString());
				}catch (Exception e) {
					log.error("短信发送失败;短信内容:"+sBuffer.toString());
					e.printStackTrace();
				}   
           }
           else if("1".equals(state)){
        	   List<SmsUsers> checkUsers =smsUpwardMapper.findCheckUsersbyDuty();
        	   List<String> checkMsisdns = new ArrayList<String>();
				for (SmsUsers checkUser : checkUsers) {
					log.info(checkUser.getMsisdn());
					checkMsisdns.add(checkUser.getMsisdn());//系统号码
				}
        	   StringBuffer sbf = new StringBuffer();
				String contents = smsTemplateAttr.getCityName()+"市项目:" + smsTemplateAttr.getName()+","+ "地市管理员:"/*+checkName*/+users.getMsisdn()+"已审核,";
				sbf.append(contents);
				//获取短信内容
				SmsContent smsContent=smsContentService.findContentByMgrId(mgrId);
				//判断新旧短信
				if(smsContent.getNewContent() == null||"".equals(smsContent.getNewContent())){
				sbf.append("短信内容如下 :"+smsContent.getContent());	
				}else{
					sbf.append("短信内容如下 :"+smsContent.getNewContent());	
				}
				sbf.append(";同意请回复:Y-"+mgrId+"-HY，不同意请回复:N-"+mgrId+"-HY");
				//发送系统管理员
				for (String string : checkMsisdns) {
					try {
						mapSend.put("receiver", string);
						mapSend.put("content", sbf.toString());
						int result1 = smsTemplateAttrService.insertSend(mapSend);
						String logString = result1==1 ? "短信发送成功" : "短信发送失败";
						log.info(logString+";短信内容为:"+sbf.toString());
					}
					catch (Exception e) {
						log.error("模板审核异常"+e.toString());
						e.printStackTrace();
					}
				}  
           }
        }
        result = smsTemplateCheckService.templateCheck(mgrId, state, remark, users.getMsisdn());
        long mgrState=Long.parseLong(smsBusiService.findByMgrId(mgrId).getState());
        if (1 == result) {
            return ResponseUtil.sucess("审核成功",mgrState);
        } else {
            return ResponseUtil.sucess("审核失败请重试");
        }
    }
}
